package com.ol.edu.server.controller.Authority;


import com.ol.edu.server.entity.Resource;
import com.ol.edu.server.entity.ResourseVo;
import com.ol.edu.server.entity.ResponseResult;
import com.ol.edu.server.service.Authority.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Roger
 * @date 2020/9/9 10:33
 * @description
 */
@RestController
@RequestMapping("/resource")
@CrossOrigin
public class ResourceController {
    @Autowired
    private ResourceService resourceService;
    /*
        资源信息分页&条件查询
     */
    @RequestMapping("/findAllResource")
    public ResponseResult findAllResource(@RequestBody ResourseVo resourseVo) {
        if (null == resourseVo.getCurrentPage()) {
            resourseVo.setCurrentPage(1);
        }
        if (null == resourseVo.getPageSize()) {
            resourseVo.setPageSize(10);
        }
        ResponseResult responseResult = resourceService.findAllResource(resourseVo);
        return responseResult;
    }
    /*
        添加&更新资源信息
     */
    @RequestMapping("/saveOrUpdateResource")
    public ResponseResult saveOrUpdateResource(@RequestBody Resource resource) {
        //如果传入的ID为空表示是新增
        if (resource.getId() == null) {
            resourceService.saveResource(resource);
            return new ResponseResult(true, 200, "添加资源成功", null);
        } else {// 更新
            resourceService.updateResource(resource);
            return new ResponseResult(true, 200, "修改资源成功", null);
        }
    }
    /*
        删除资源信息
     */
    @RequestMapping("/deleteResource")
    public ResponseResult deleteResource(Integer id) {
        resourceService.deleteResource(id);
        return new ResponseResult(true, 200, "删除资源成功", null);
    }



}
